﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class ListWaitBLL
    {
        HospitalDataContext DB = new HospitalDataContext();
        private static int listid = 0;
        public IQueryable SearchALLWithPatient()
        {
            var kq = (from lw in DB.ListWaits
                      join appoint in DB.Appointments
                      on lw.AppointmentID equals appoint.AppointmentID into dss
                      from ds in dss.DefaultIfEmpty()
                      join pa in DB.Patients
                      on ds.PatientID equals pa.PatientID into ketquas
                      from ketqua in ketquas.DefaultIfEmpty()
                      select new
                      {
                          ListID = lw.ListID,
                          AppointmentID = ds.AppointmentID,
                          WardID = lw.WardID,
                          PatientID = ketqua.PatientID,
                          DateRegistered = lw.DateRegistered,
                          Status = lw.Status,

                      }).Distinct();
            return kq;
        }
        public IQueryable SearchWithPatient(string key)
        {
            var kq = (from lw in DB.ListWaits
                      join appoint in DB.Appointments
                      on lw.AppointmentID equals appoint.AppointmentID into dss
                      from ds in dss.DefaultIfEmpty()
                      join pa in DB.Patients
                      on ds.PatientID equals pa.PatientID into ketquas
                      from ketqua in ketquas.DefaultIfEmpty()
                      where ketqua.FirstName.Contains(key) || ketqua.LastName.Contains(key) || lw.WardID.Contains(key)
                      select new
                      {
                          ListID = lw.ListID,
                          AppointmentID = ds.AppointmentID,
                          WardID = lw.WardID,
                          PatientID = ketqua.PatientID,
                          Status = lw.Status,
                          lw.DateRegistered
                      }).Distinct();
            return kq;
        }
        public string Insert(ListWaitDTO nhap)
        {
            try
            {
                ListWait list = new ListWait();
                list.ListID = (listid++).ToString();
                list.AppointmentID = nhap.AppointmentID;
                list.WardID = nhap.WardID;
                list.DateRegistered = nhap.DateRegistered;
                list.Status = nhap.Status;

                DB.ListWaits.InsertOnSubmit(list);
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e) {
                return e.Message;
            }
        }
        public string Update_to_inpa(string listid)
        {
            try
            {
                ListWait lw = DB.ListWaits.Where(l => l.ListID == listid).FirstOrDefault();
                lw.Status = "inpa";
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception ee)
            {
                return ee.Message;
            }
        }
        public string Get_WardID(string listid)
        {
            ListWait lw = DB.ListWaits.Where(l => l.ListID == listid).FirstOrDefault();
            return lw.WardID;
        }
    }
}
